package com.hui.dao.impl;

import com.hui.bean.ClassBean;
import com.hui.bean.RoomBean;
import com.hui.bean.StudentBean;
import com.hui.dao.IRecordDao;
import com.hui.dao.IRoomDao;
import com.hui.dao.IStudentDao;
import com.hui.util.DBUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RoomDaoImpl implements IRoomDao {
    /**
     * 得到所有的班级对象集合
     * @return  返回所有班级对象集合
     * @throws SQLException
     */
    @Override
    public List<RoomBean> getAllRooms() throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        List<RoomBean> roomBeanList = mapper.getAllRooms();
        session.close();
        return  roomBeanList;
    }

    @Override
    public List<RoomBean> getRoomsByRoomType(String roomType) {
        return null;
    }

    /**
     * 通过条件查询符合条件的房间对象集合
     * @param roomAddress 房间地址
     * @param roomStatus 房间状态 正常/损坏
     * @param availableLive 是否可住 (是否满员)  0：房间满员  1：房间人数未满
     * @param roomType  房间类型 男寝室/女寝室
     * @return  符合条件的房间对象集合
     * @throws SQLException
     */
    @Override
    public List<RoomBean> findRoomsByCondition(String roomAddress, String roomStatus, String availableLive, String roomType) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        List<RoomBean> roomBeanList = mapper.findRoomsByCondition(roomAddress, roomStatus, availableLive, roomType);
        session.close();
        return  roomBeanList;
    }

    /**
     * 添加一个学生
     * @param roomBean  带数据的房间信息对象
     * @return  受影响行数
     * @throws SQLException
     */
    @Override
    public int saveRoom(RoomBean roomBean) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        int result = mapper.saveRoom(roomBean);
        session.commit();
        session.close();
        return result;
    }
    /**
     * 通过房间id删除一个房间
     * 1.通过房间id查询该房间是否还有学生
     * 2.如果没有学生，直接删除；如果有学生，就不删
     * @param roomId 房间id
     * @return 受影响行数
     * @throws SQLException
     */
    @Override
    public int deleteRoomByRoomId(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        List<StudentBean> studentBeanList = studentDao.getStudentByRoomId(roomId);
        if (studentBeanList.isEmpty()){
            roomDao.deleteRoomByRoomId(roomId);
            session.commit();
            session.close();
            return 1;
        }else {
            session.close();
            return 0;
        }

    }

    /**
     * 通过房间id得到对应的房间对象
     * @param roomId  房间id
     * @return 对应的房间对象
     * @throws SQLException
     */
    @Override
    public RoomBean getRoomByRoomId(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        RoomBean roomBean = mapper.getRoomByRoomId(roomId);
        session.close();
        return roomBean;
       }

    /**
     * 通过性别得到对应性别和人数未满的房间对象集合
     * @param gender  性别
     * @return 符合条件的房间对象集合
     * @throws SQLException
     */
    @Override
    public List<RoomBean> getAvailableRooms(String gender) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        List<RoomBean> roomBeanList = mapper.getAvailableRooms(gender);
        session.close();
        return roomBeanList;
  }

    @Override
    public List<RoomBean> getChangeableRooms(String gender) {
        return null;
    }

    /**
     *
     * @param roomAddress
     * @return
     * @throws SQLException
     */
    @Override
    public RoomBean getRoomByRoomAddress(String roomAddress) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        RoomBean roomBean = mapper.getRoomByRoomAddress(roomAddress);
        session.close();
        return roomBean;

  }

    @Override
    public int removeStudentFromRoomByRoomId(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        int result = mapper.removeStudentFromRoomByRoomId(roomId);
        session.commit();
        session.close();
        return result;
    }

    @Override
    public int addStudentInRoomByRoomId(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        int result = mapper.addStudentInRoomByRoomId(roomId);
        session.commit();
        session.close();
        return result;
 }

    @Override
    public int updateRoomStatusToError(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRoomDao mapper = session.getMapper(IRoomDao.class);
        int result = mapper.updateRoomStatusToError(roomId);
        session.commit();
        session.close();
        return result;
  }

    @Override
    public int updateRoomStatusToNormal(int roomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IRecordDao recordDao = session.getMapper(IRecordDao.class);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        int account = recordDao.getRecordStatusIsZeroAccountByRoomId(roomId);
        if (account!=0){
            session.close();
            return 0;
        }else {
            roomDao.updateRoomStatusToNormal(roomId);
            session.commit();
            session.close();
            return 1;
           }

    }

}
